<?php
namespace App\Controller\App;

use App\Repository\UserRepository;
use App\Repository\GoodsRepository;
use App\Repository\GoodsOrderRepository;
use App\Repository\ConfigRepository;

use App\Event\GoodsOrderCreateEvent;

class GoodsOrderController extends Controller
{
    // 创建订单
    public function create($id)
    {
        // 获取商品
        if(!$goods = GoodsRepository::find($id))
        {
            $this->error('empty');
        }
        
        // 可用
        if(!$goods->isStatus())
        {
            $this->error('goods_status');
        }
        
        // 代购用户激活或自己
        if(!$user = $this->userActive())
        {
            $user = $this->auth();
        }
        
        // 等级判断
        if(!$goods->level($user->level_id))
        {
            $this->error('goods_level');
        }
        
        // 校验金额
        $money = $this->check($goods->money, (float) $this->input('wallet_reg'), (float) $this->input('wallet_option'));
        
        // 开始支付
        $this->pay('购买【' . $goods->name . '】 扣除', $money);
        
        // 创建订单
        if(GoodsOrderRepository::make()->create($user, $goods, $money))
        {
            $this->message('ok');
        }
        
        $this->error('error');
    }
    
    // 激活用户
    protected function userActive()
    {
        // 附加用户
        $user_id = $this->input('user_id');
        
        // 取出用户
        if($user = UserRepository::find($user_id))
        {
            // 是否激活
            if($user->isActive())
            {
                $this->error('active');
            }
            
            return $user;
        }
    }
    
    // 校验金额
    protected function check($money, $wallet_reg, $wallet_option)
    {
        // 算出总优惠
        $active = $wallet_reg + $wallet_option;
        
        // 算出余额
        $wallet_full = $money - $active;
        
        // 判断期权不能超过 5%
        if(ConfigRepository::bi('money_active_option', $money) < $wallet_option)
        {
            $this->error('goods_money_active_option');
        }
        
        // 判断使用的优惠，不能超过 50%
        if(ConfigRepository::bi('money_active', $money) < $active)
        {
            $this->error('goods_money_active');
        }
        
        return compact('wallet_full', 'wallet_reg', 'wallet_option');
    }
    
    // 开始支付
    protected function pay($content, $money)
    {
        // 扣掉余额
        if(!$this->auth()->isAccount('wallet_full', $money['wallet_full']))
        {
            $this->error('wallet_full');
        }
        
        // 扣掉优惠
        if(!$this->auth()->isAccount('wallet_reg', $money['wallet_reg']))
        {
            $this->error('wallet_reg');
        }
        
        // 扣掉期权
        if(!$this->auth()->isAccount('wallet_option', $money['wallet_option']))
        {
            $this->error('wallet_option');
        }
        
        $this->auth()->account('wallet_reg', '-' . $money['wallet_reg'], $content);
        $this->auth()->account('wallet_full', '-' . $money['wallet_full'], $content);
        $this->auth()->account('wallet_option', '-' . $money['wallet_option'], $content);
    }
    
    // 我的订单
    public function user()
    {
        return $this->auth()->model()->goodsOrder()->latest()->paginate(10)->toArray();
    }
    
    // 自提成功
    public function ok($id)
    {
        // 获取订单
        if($order = GoodsOrderRepository::find($id))
        {
            // 权限
            if($order->can($this->auth()))
            {
                // 自提成功
                if($order->ok($this->input('sign')))
                {
                    // 事件
                    $this->event(new GoodsOrderCreateEvent($this->auth(), GoodsRepository::make($order->goods), '购买商品'));
                    
                    $this->message('ok');
                }
            }
        }
        
        $this->error('error');
    }
}
